﻿using System;
using System.Text.RegularExpressions;

namespace Mud.Characters
{
    public class Character : ICharacter
    {
        #region member variables
        private string _name;
        #endregion

        #region Properties
        public string Name
        {
            get
            { return _name; }
            private set
            {
                if (isValidName(value))
                    _name = value;
                else throw new BadNameException(value);
            }
        }

        public string Password { get; set; }

        #endregion

        #region constructors
        public Character() : this(new CharacterDal(), String.Empty)
        {
        }

        public Character(string name, string password)
        {
            Name = name;
        }

        public Character(ICharacterDal dal, string name) : this(dal.GetCharacter(name))
        {
        }

        private Character(CharacterDataModel cdm)
        {
            if (cdm != null)
            {
                Name = cdm.Name;
                Password = cdm.Password;
            }
        }

        #endregion

        public static bool CharacterExists(ICharacterDal dal, string name)
        {
            return dal.CharacterExists(name);
        }

        private bool isValidName(string name)
        {
            if (String.IsNullOrEmpty(name) || String.IsNullOrWhiteSpace(name))
            { return false; }
            Regex r = new Regex($@"^[a-zA-z]+$");
            return r.IsMatch(name);
        }

        public bool PasswordConfirm(string password)
        {
            return true;
        }
    }
}
